<?php

    class Application_Form_MyaccountForm extends Zend_Form
    {

        private $user_id;

        public function setUserid($arg)
        {
            $this->user_id = $arg;
        }

        public function init()
        {
            $commonModel = new Application_Model_Common;

            if (!empty($this->user_id))
            {
                $record = $commonModel->getdata(RKN_USER, 'rkn_uid = "' . $this->user_id . '" ');
            }

            $this->addElement("text", "username", array(
                "placeholder" => "username",
                "class" => "form-control form-customized",
                "required" => "true",
                "filters" => array("StringTrim"),
                "validators" => array("NotEmpty", array('Db_NoRecordExists', true, array('table' => RKN_USER, 'field' => 'username', 'exclude' => array('field' => 'rkn_uid', 'value' => $this->user_id))))
            ));

            $this->username->setValue($record['username']);


            $this->addElement("text", "first_name", array(
                "placeholder" => "First Name",
                "class" => "form-control form-customized",
                "required" => "true",
                "filters" => array("StringTrim"),
                "validators" => array("NotEmpty")
            ));

            $this->first_name->setValue($record['first_name']);

            $this->addElement("text", "last_name", array(
                "placeholder" => "Last Name",
                "class" => "form-control form-customized",
                "required" => "true",
                "filters" => array("StringTrim"),
                "validators" => array("NotEmpty")
            ));

            $this->last_name->setValue($record['last_name']);

            $date = $record['dob'];
            if (!empty($date) && $date != '0000-00-00')
            {
                $day = date('d', strtotime($date));
                $month = date('m', strtotime($date));
                $year = date('Y', strtotime($date));
            }
            else
            {
                $day = $month = $year = '';
            }
            $this->addElement("text", "month", array(
                "placeholder" => "MM",
                "class" => "form-control form-customized month pull-left",
                "required" => "true",
                "style" => "margin-right:10px;",
                "filters" => array("StringTrim"),
                "validators" => array(array("name"=>"NotEmpty","break_chain_on_failure"=>true), "Digits", array('Between', false, array('min' => '01', 'max' => '12')))
            ));

            $this->month->setValue($month);

            $this->addElement("text", "day", array(
                "placeholder" => "DD",
                "class" => "form-control form-customized month pull-left",
                "required" => "true",
                "style" => "margin-right:10px;",
                "filters" => array("StringTrim"),
                "validators" => array("NotEmpty", "Digits", array('Between', false, array('min' => '01', 'max' => '31')))
            ));

            $this->day->setValue($day);


            $this->addElement("text", "year", array(
                "placeholder" => "YYYY",
                "class" => "form-control form-customized year pull-left",
                "required" => "true",
                "filters" => array("StringTrim"),
                "validators" => array("NotEmpty", "Digits", array('LessThan', false, array('max' => date('Y') - 18)))
            ));

            $this->day->setValue($year);

            $this->addElement("text", "address", array(
                "placeholder" => "Address",
                "class" => "form-control form-customized",
                "required" => "true",
                "maxlength" => "100",
                "filters" => array("StringTrim"),
                "validators" => array("NotEmpty")
            ));

            $this->address->setValue($record['address']);

            $this->addElement("text", "city", array(
                "placeholder" => "City",
                "class" => "form-control form-customized",
                "required" => "true",
                "maxlength" => "50",
                "filters" => array("StringTrim")
            ));

            $this->city->setValue($record['city']);

            $this->addElement("text", "state", array(
                "placeholder" => "State",
                "class" => "form-control form-customized",
                "required" => "true",
                "maxlength" => "50",
                "filters" => array("StringTrim")
            ));

            $this->state->setValue($record['state']);

            $this->addElement("text", "zip_code", array(
                "placeholder" => "State",
                "class" => "form-control form-customized",
                "required" => "true",
                "maxlength" => "50",
                "filters" => array("StringTrim"),
                "validators" => array("NotEmpty", "Alnum")
            ));

            $this->zip_code->setValue($record['zip_code']);

            $this->addElement("text", "country", array(
                "placeholder" => "Country",
                "class" => "form-control form-customized",
                "required" => "true",
                "maxlength" => "50",
                "filters" => array("StringTrim"),
                "validators" => array("NotEmpty")
            ));

            $this->country->setValue($record['location']);

            $this->addElement("checkbox", "promotion_mail", array(
                "class" => "pull-left"
            ));

            $this->promotion_mail->setValue($record['promotion_mail']);

            $this->addElement("checkbox", "updates_mail", array(
                "class" => "pull-left"
            ));

            $this->updates_mail->setValue($record['updates_mail']);

            $this->addElement("submit", "submit_button", array(
                "label" => "GO",
                "class" => "btn-register btn-block"
            ));

            $elements = $this->getElements();
            foreach ($elements as $element)
            {
                $element->removeDecorator("DtDdWrapper")->removeDecorator("HtmlTag")->removeDecorator("Label")->removeDecorator("Errors");
            }

            $this->setDecorators(array(
                'FormErrors',
                new Zend_Form_Decorator_FormErrors(array(
                    'ignoreSubForms' => true,
                    'markupElementLabelEnd' => '</b>',
                    'markupElementLabelStart' => '<b>',
                    'markupListEnd' => '</div>',
                    'markupListItemEnd' => '</span>',
                    'markupListItemStart' => '<span>',
                    'markupListStart' => '<div'
                )),
                'Form'
            ));
        }

    }

    